package com.java.action;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.java.domain.PatientDo;
import com.java.domain.UserDo;
import com.java.service.PatientService;
import com.java.utils.JsonUtils;
import com.java.utils.StrUtils;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

@Controller
@Scope("prototype")
public class PatientManageAction extends ActionSupport implements
		ModelDriven<PatientDo> {

	private static final long serialVersionUID = 12L;

	@Resource
	private PatientService patientService;


	public String showPatientManage() {
		return "showPatientManage";
	}
	
	public String showDeltail(){
		HttpServletRequest request = ServletActionContext.getRequest();
		String id = request.getParameter("id");
		UserDo userDo = (UserDo) request.getSession().getAttribute("userDo");
		PatientDo patient = patientService.getById(Integer.parseInt(id));
		ActionContext.getContext().put("patient",patient);
		ActionContext.getContext().put("userDo",userDo);
		return "showDeltail";
	}

	public String showDel() {
		return "showDel";
	}
	
	public void showPatientDel() {
		HttpServletRequest request = ServletActionContext.getRequest();
		UserDo userDo = (UserDo) request.getSession().getAttribute("userDo");
		if (userDo != null) {
			List<PatientDo> plist = patientService.queryPatient(userDo.getId());
			for (PatientDo pdo : plist) {
				SimpleDateFormat format = new SimpleDateFormat(
						"yyyy-MM-dd HH:mm:ss");
				pdo.setDateFormmat(format.format(pdo.getPjzsj()));
			}
			JsonUtils.JsonTostring(plist);
		}
	}

	public void showOnePatient() {
		HttpServletRequest request = ServletActionContext.getRequest();
		String uid = request.getParameter("uid");
		if (StrUtils.isNotEmpty(uid)) {
			List<PatientDo> plist = patientService.queryPatient(Integer
					.parseInt(uid));
			for (PatientDo pdo : plist) {
				SimpleDateFormat format = new SimpleDateFormat(
						"yyyy-MM-dd HH:mm:ss");
				pdo.setDateFormmat(format.format(pdo.getPjzsj()));
			}
			JsonUtils.JsonTostring(plist);
		}

	}

	public void save() {
		Map<String, Object> map = new HashMap<String, Object>();
		HttpServletRequest request = ServletActionContext.getRequest();
		String suggest = request.getParameter("suggest");
		String desc = request.getParameter("desc");
		String uid = request.getParameter("uid");
		String lrys = request.getParameter("lrys");
		String sxks = request.getParameter("sxks");
		String content = request.getParameter("content");
		String result = request.getParameter("result");
		if (StrUtils.isNotEmpty(uid) && StrUtils.isNotEmpty(desc)) {
			List<PatientDo> patientList = patientService.queryPatient(Integer
					.parseInt(uid));
			PatientDo patient = null;
			boolean flag = false;
			if (patientList != null && patientList.size() > 0) {
				for (PatientDo tpd : patientList) {
					if (tpd.getPdesc() == null) {
						patient = tpd;
						patient.setPks(sxks);
						patient.setPdoctor(lrys);
						patient.setPcontent(content);
						patient.setPresult(result);
						patient.setPdesc(desc);
						patient.setPsuggest(suggest);
						patient.setPjzsj(new Date());
						patientService.update(patient);
						flag = true;
						break;
					}
				}
			}
			if (!flag) {
				PatientDo tempPatient = new PatientDo();
				tempPatient.setPuid(Integer.parseInt(uid));
				tempPatient.setPks(sxks);
				tempPatient.setPdoctor(lrys);
				tempPatient.setPcontent(content);
				tempPatient.setPresult(result);
				tempPatient.setPdesc(desc);
				tempPatient.setPsuggest(suggest);
				tempPatient.setPjzsj(new Date());
				patientService.save(tempPatient);
			}
			map.put("success", "success");
		} else {
			map.put("error", "error");
		}
		JsonUtils.JsonTostring(map);
	}

	public PatientDo getModel() {
		return null;
	}

}
